package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.dtos.AuthUpdateDto;
import com.heima.model.user.pojos.ApUserAuth;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserAuthService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserAuth> implements ApUserAuthService {

    @Autowired
    private ApUserService apUserService;
    /**
     * 审核列表查询
     * @param dto
     */
    @Override
    public ResponseResult authList(AuthDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        IPage page = new Page(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<ApUserAuth> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (dto.getStatus() != null){
            lambdaQueryWrapper.eq(ApUserAuth::getStatus,dto.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(ApUserAuth::getCreatedTime);

        page(page,lambdaQueryWrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int) page.getTotal());
         return responseResult.ok(page.getRecords());
    }

    /**
     * 审核
     * @param dto
     * @param userAuthStatus
     * @return
     */
    @Override
    public ResponseResult authUpdate(AuthUpdateDto dto, Short userAuthStatus) {
        if (dto ==  null || dto.getId() == null || userAuthStatus == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserAuth apUserAuth = getById(dto.getId());
        apUserAuth.setStatus(userAuthStatus);
        apUserAuth.setReason(dto.getMsg());
        updateById(apUserAuth);
        apUserService.userAuthKeep(apUserAuth.getUserId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
